package com.ailiwu.www.ailiwu.Fragment;

import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.ailiwu.www.ailiwu.R;
import com.ailiwu.www.ailiwu.adapter.GiftRecyclerViewAdapter;
import com.ailiwu.www.ailiwu.base.BaseFragment;
import com.ailiwu.www.ailiwu.bean.ClassifyGift;
import com.ailiwu.www.ailiwu.url.Urls;
import com.ailiwu.www.ailiwu.utils.Logger;
import com.ailiwu.www.ailiwu.utils.ToastUtils;
import com.ailiwu.www.ailiwu.views.GigtListView;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zzw on 2015/12/3.
 */
public class ClassifyGiftFragmnet extends BaseFragment {
    private RelativeLayout mRlGiftTop;
    private ClassifyGift mCfg;
    private ScrollView scGift;
    private LinearLayout llGift;
    //左面的listView
    private com.ailiwu.www.ailiwu.views.GigtListView mLvGiftLeft;
    private List<ClassifyGift.DataEntity.CategoriesEntity> mCategories;
    private TitleAdapter mTitleAdapter;
    //右面RecyclerView
    private RecyclerView mRvGiftRight;
    private GiftRecyclerViewAdapter mGiftAdapter;

    private View lastView;
//    private View lastView2;

    private boolean isLeft = true;
    private boolean isRight = true;
    int height = 0;


    @Override
    protected View inflateView() {
        return View.inflate(getActivity(), R.layout.fragment_classify_gift, null);
    }

    @Override
    protected void initView(View view) {
        mRlGiftTop = (RelativeLayout) view.findViewById(R.id.rl_gift_top);
        mLvGiftLeft = (GigtListView) view.findViewById(R.id.lv_gift_left);
        mRvGiftRight = (RecyclerView) view.findViewById(R.id.rv_gift_right);
        llGift = (LinearLayout) view.findViewById(R.id.ll_gift);
        scGift = (ScrollView) view.findViewById(R.id.scroll_gift);
    }

    @Override
    protected void initData() {

        registerAdapter();

        loadData();


        setListener();


    }






    private void setListener() {


        mLvGiftLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Logger.i("左边滑到=======" + position);
                isRight = false;
                if (lastView != null) {
                    lastView.setBackgroundColor(Color.parseColor("#FAFAFA"));
                    TextView tmp = (TextView) lastView.findViewById(R.id.tv_item_giftlist);
                    tmp.setTextColor(Color.parseColor("#000000"));
                    ImageView imageView = (ImageView) lastView.findViewById(R.id.iv_giftlist);
                    imageView.setVisibility(View.INVISIBLE);
                    mRvGiftRight.scrollToPosition(position);
                }
                view.setBackgroundColor(Color.parseColor("#FFFFFF"));
                TextView tmp = (TextView) view.findViewById(R.id.tv_item_giftlist);
                tmp.setTextColor(Color.parseColor("#F84E4E"));
                ImageView imageView = (ImageView) view.findViewById(R.id.iv_giftlist);
                imageView.setVisibility(View.VISIBLE);

                lastView = view;


            }
        });

        mRvGiftRight.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                switch (newState) {
                    case RecyclerView.SCROLL_STATE_IDLE:

                        break;
                    case RecyclerView.SCROLL_STATE_DRAGGING:
                        isRight = true;

                        break;
                    case RecyclerView.SCROLL_STATE_SETTLING:

                        break;
                    default:

                        break;
                }

            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                Logger.i("=-=-=-=-=-=-==-=-=-=-dy" + dy);

                LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                if (isRight) {
                    if (lastView != null) {
                        lastView.setBackgroundColor(Color.parseColor("#FAFAFA"));
                        TextView tmp = (TextView) lastView.findViewById(R.id.tv_item_giftlist);
                        tmp.setTextColor(Color.parseColor("#000000"));
                        ImageView imageView = (ImageView) lastView.findViewById(R.id.iv_giftlist);
                        imageView.setVisibility(View.INVISIBLE);
                    }

                    int a = manager.findFirstVisibleItemPosition();
                    Logger.i("右边滑到-------" + a);

                    View view = mLvGiftLeft.getChildAt(a);
                    view.setBackgroundColor(Color.parseColor("#FFFFFF"));
                    TextView tmp = (TextView) view.findViewById(R.id.tv_item_giftlist);
                    tmp.setTextColor(Color.parseColor("#F84E4E"));//变红
                    ImageView imageView = (ImageView) view.findViewById(R.id.iv_giftlist);
                    imageView.setVisibility(View.VISIBLE);
                    int tmpheight = view.getHeight();

                    if(dy>0 && a == (int)mLvGiftLeft.getCount()/2){

                        scGift.smoothScrollTo(0, tmpheight*20);
                    }else if(dy < 0 && a ==(int)mLvGiftLeft.getCount()/2){
                        scGift.smoothScrollTo(0, -tmpheight*20);
                    }
                    mLvGiftLeft.smoothScrollToPosition(a);
                    lastView = view;
                }
            }




        });
    }


    private void loadData() {
        //设置连接超时时间
        httpUtils.configTimeout(30000);
        //设置缓存区中数据的存储时间，单位为毫秒（一旦超出指定时间，缓存内的数据会被清空）
        httpUtils.configDefaultHttpCacheExpiry(24 * 3600 * 1000);
        //设置缓存区的大小，单位是b字节
        httpUtils.configHttpCacheSize(4 * 1024 * 1024);

        httpUtils.send(HttpRequest.HttpMethod.GET,
                Urls.CLASSIFY_GIFT_RECYC,
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String result = responseInfo.result;
                        mCfg = gson.fromJson(result, ClassifyGift.class);
                        Logger.i("__________mCfg" + mCfg.toString());
                        //左面listview的数据加载
                        mCategories = new ArrayList<ClassifyGift.DataEntity.CategoriesEntity>();
                        mCategories.addAll(mCfg.getData().getCategories());
                        //下载完更新数据
                        mTitleAdapter.notifyDataSetChanged();

                        //右面RecyclerView数据加载
                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
                        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                        mRvGiftRight.setLayoutManager(linearLayoutManager);
                        mGiftAdapter = new GiftRecyclerViewAdapter(getContext(), mCfg);
                        mRvGiftRight.setAdapter(mGiftAdapter);
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        ToastUtils.showShort(getContext(), "网络连接出错，请检查网络！");
                    }
                }
        );
    }

    private void registerAdapter() {
        mTitleAdapter = new TitleAdapter();
        mLvGiftLeft.setAdapter(mTitleAdapter);


    }

    public class TitleAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mCategories == null ? 0 : mCategories.size();
        }

        @Override
        public Object getItem(int position) {
            return mCategories.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
//            VHolder holder;
//            if(convertView == null){
            View tmpView = View.inflate(mAcAvtivity, R.layout.item_gift_list, null);
//                holder = new VHolder();
            TextView tvTitle = (TextView) tmpView.findViewById(R.id.tv_item_giftlist);
            ImageView imageView = (ImageView) tmpView.findViewById(R.id.iv_giftlist);
//                convertView.setTag(holder);
//            }else{
//                holder = (VHolder) convertView.getTag();
//            }

            ClassifyGift.DataEntity.CategoriesEntity tmp = mCategories.get(position);

            tvTitle.setText(tmp.getName());
//            if(isFirst){
//                convertView.setBackgroundColor(Color.parseColor("#FFFFFF"));
//                tvTitle.setTextColor(Color.parseColor("#F84E4E"));
//                imageView.setVisibility(View.VISIBLE);
//                lastView = convertView;
//                isFirst = false;
//
//            }


            return tmpView;
        }


//        class VHolder{
//            TextView tvTitle;
//            ImageView imageView;
//        }
    }

}
